home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 16 / AMIGAplus Sonderheft 16 (1998)(ICP)(DE)[!].iso / pd / anwendungen / xpk_source / test / testchunkyunpack.c < prev    next >
C/C++ Source or Header  |  1998-08-27  |  4KB  |  175 lines

  1. #define NAME        "testChunkyUnPack"
  2. #define DISTRIBUTION    "(Freeware) "
  3. #define REVISION    "0"
  4.  
  5. /* Programmheader
  6.  
  7.     Name:        testChunkyUnPack
  8.     Author:        SDI
  9.     Distribution:    Freeware
  10.     Description:    tests chunky unpacking using XpkRead
  11.     Compileropts:    -
  12.     Linkeropts:    -l xpkmaster amiga
  13.  
  14.  1.0   27.08.98 : first version
  15. */
  16.  
  17. /*
  18. For correct working you need to use at least xpkmaster.library 4.34! Either
  19. ensure, that this library is installed, or check after opening
  20.   if(XpkBase->lib_Version > 4 || XpkBase->lib_Revision >= 34)
  21.   { ... do the work ... }
  22.   else
  23.   { ... do some error stuff ... }
  24. */
  25.  
  26. #include <proto/exec.h>
  27. #include <proto/dos.h>
  28. #include <proto/xpkmaster.h>
  29. #include <exec/memory.h>
  30. #include <exec/execbase.h>
  31. #include "SDI_defines.h"
  32.  
  33. struct Library        *XpkBase        = 0;
  34. ULONG            DosVersion        = 37;
  35.  
  36. #define PARAM "FROM/A"
  37.  
  38. struct Args {
  39.   STRPTR from;
  40. };
  41.  
  42. struct BufData {
  43.   STRPTR       buffer;
  44.   ULONG         start;
  45.   ULONG         filled;
  46.   LONG         err;
  47.   struct XpkFib *xfib;
  48. };
  49.  
  50. LONG ReadData(struct BufData *b, APTR buf, ULONG size);
  51.  
  52. void main(void)
  53. {
  54.   struct Args args;
  55.   struct RDArgs *rda;
  56.  
  57.   if((rda = ReadArgs(PARAM, (LONG *) &args, 0)))
  58.   {
  59.     if((XpkBase = OpenLibrary(XPKNAME, 4)))
  60.     {
  61.       struct BufData b;
  62.       LONG err;
  63.  
  64.       if(!(err = XpkOpenTags(&b.xfib, XPK_InName, args.from, TAG_DONE)))
  65.       {
  66.         if((b.buffer = (STRPTR) AllocVec(b.xfib->xf_NLen, MEMF_ANY)))
  67.         {
  68.           b.start = 0;
  69.           b.filled = 0;
  70.           b.err    = 0;
  71.  
  72.       /* this is a test, loading useless stuff saved with textChunkyPack */
  73.       {
  74.         ULONG buf[50];
  75.  
  76.         /* because of delayed error, we need not test all calls! */
  77.         if((err = ReadData(&b, buf, 8)))
  78.           XpkPrintFault(err, 0);
  79.         else if(buf[0] == 0x49445854 && buf[1] == 0x43503130)
  80.         {
  81.           Printf("ID value IDXTCP10 is correct\n");
  82.  
  83.           if((err = ReadData(&b, buf, 21)))
  84.             XpkPrintFault(err, 0);
  85.           else
  86.           {
  87.             Printf("%s\n", buf);    /* dosbase text */
  88.             ReadData(&b, 0, sizeof(struct DosLibrary));
  89.             if((err = ReadData(&b, buf, 21)))
  90.               XpkPrintFault(err, 0);
  91.             else
  92.             {
  93.               Printf("%s\n", buf); /* sysbase text */
  94.               ReadData(&b, 0, sizeof(struct ExecBase));
  95.               if((err = ReadData(&b, buf, 21)))
  96.                 XpkPrintFault(err, 0);
  97.               else
  98.               {
  99.                 ULONG a = *((ULONG *)(((STRPTR) 0x1000000)-20));
  100.                 Printf("%s\n", buf); /* kickstart text */
  101.                 if((err = ReadData(&b, 0, a-20)))
  102.                   XpkPrintFault(err, 0);
  103.                 else
  104.                 {
  105.                   if((err = ReadData(&b, buf, 4)))
  106.                     XpkPrintFault(err, 0);
  107.                   else
  108.                     Printf("Kickstart size is correct: %ld\n", buf[0]);
  109.                   Printf("Now read too much and produce an error\n");
  110.                   if((err = ReadData(&b, 0, 10000)))
  111.                     XpkPrintFault(err, 0);
  112.                 }
  113.               }
  114.             }
  115.           }
  116.         }
  117.         else
  118.           Printf("Wrong ID\n");
  119.       }
  120.           
  121.           FreeVec(b.buffer);
  122.         }
  123.         XpkClose(b.xfib);
  124.       }
  125.       else
  126.         XpkPrintFault(err, 0);
  127.       CloseLibrary(XpkBase);
  128.     }
  129.     FreeArgs(rda);
  130.   }
  131. }
  132.  
  133. /* Call this function for reading stored data, calling it with buffer zero
  134. means skipping some data.
  135.  
  136. This function uses a delayed error report, so we need not check every call.
  137. ReadData does nothing except returning the error again, when an error
  138. occured a call before. */
  139. LONG ReadData(struct BufData *b, APTR buf, ULONG size)
  140. {
  141.   LONG i;
  142.  
  143.   while(size && !b->err)
  144.   {
  145.     if(b->filled == b->start)
  146.     {
  147.       if((i = XpkRead(b->xfib, b->buffer, b->xfib->xf_NLen)) < 0)
  148.         b->err = i;
  149.       else
  150.       {
  151.     if(!i)    /* we reached the end */
  152.       b->err = XPKERR_IOERRIN;
  153.         b->filled = i;
  154.         b->start = 0;
  155.       }
  156.     }
  157.     if(!b->err)
  158.     {
  159.       if((i = b->filled - b->start) > size)
  160.         i = size;
  161.  
  162.       if(buf)
  163.       {
  164.         CopyMem(b->buffer+b->start, buf, i);
  165.         buf = ((STRPTR) buf) + i;
  166.       }
  167.       b->start += i;
  168.       size -= i;
  169.     }
  170.   }
  171.  
  172.   return b->err;
  173. }
  174.  
  175.